package com.umeox.ota.utils;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;

import androidx.core.app.NotificationCompat;

import com.abupdate.trace.Trace;
import com.umeox.ota.BuildConfig;
import com.umeox.ota.R;
import com.umeox.ota.data.local.CustomConfig;
import com.umeox.ota.gloable.App;
import com.umeox.ota.ui.activity.MainActivity;
import com.umeox.ota.ui.activity.NoticeActivity;

/**
 * @author fighter_lee
 * @date 2018/9/4
 */
public class NotifyUtils {

    public static final long FORCE_INSTALL_SHOW_TIME = 10;
    private static final String TAG = "NotifyUtils";
    private @SuppressLint("WrongConstant")
    static NotificationManager mNotificationManager = (NotificationManager) App.sCx.getSystemService(Context.NOTIFICATION_SERVICE);
    private static NotificationCompat.Builder mBuilder;

    public static void notifyNewVersion() {
        String notifyType = CustomConfig.getInstance().notifyType;
        if (BuildConfig.FLAVOR.contains("Service")) {
            //无屏版本只能使用通知栏提示
            notifyType = CustomConfig.NotifyType.NOTIFICATION_NOTIFY.getType();
        }
        if (CustomConfig.NotifyType.DIALOG_NOTIFY.getType().equals(notifyType)) {
            showNotifyDialog(NoticeActivity.NOTICE_TYPE_SHOW_NEW_VERSION);
        } else {
            showNotification(App.sCx.getString(R.string.notify_tips));
        }
    }

    public static void notifyDownloadFinish(boolean forceInstall) {
        String notifyType = CustomConfig.getInstance().notifyType;
        if (BuildConfig.FLAVOR.contains("Service")) {
            //无屏版本只能使用通知栏提示
            notifyType = CustomConfig.NotifyType.NOTIFICATION_NOTIFY.getType();
        }
        if (CustomConfig.NotifyType.DIALOG_NOTIFY.getType().equals(notifyType)) {
            showNotifyDialog(forceInstall ? NoticeActivity.NOTICE_TYPE_SHOW_WILL_INSTALL_FORCE : NoticeActivity.NOTICE_TYPE_SHOW_DOWNLOAD_FINISH);
        } else {
            showNotification(forceInstall ? App.sCx.getString(R.string.tips_update_prompt) : App.sCx.getString(R.string.tips_update_normal));
        }
    }

    private static void showNotifyDialog(int noticeType) {
        if (ActivityCollector.isShowing()) {
            //设备已在前台显示
            Trace.d(TAG, "App is in the foreground display");
            return;
        }
        Intent intent = new Intent(App.sCx, NoticeActivity.class);
        intent.putExtra(NoticeActivity.NOTICE_TYPE, noticeType);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        App.sCx.startActivity(intent);
    }

    private static void showNotification(String notifyTips) {
        mBuilder = new NotificationCompat.Builder(App.sCx)
                .setSmallIcon(R.mipmap.ic_launcher_white)
                .setColor(App.sCx.getResources().getColor(R.color.accent))
                .setLargeIcon(BitmapFactory.decodeResource(App.sCx.getResources(), R.mipmap.ic_launcher))
                .setContentTitle(App.sCx.getString(R.string.iot_main_title))
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setAutoCancel(true)
                .setContentText(notifyTips);
        Intent resultIntent = new Intent(App.sCx, MainActivity.class);
        //去掉 single task 类型的  launch mode 之后通过通知栏打开 activity 会出现多个 activity 实例, 所以需要 clear top , 暂不清楚有何风险
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(App.sCx, 0, resultIntent, 0);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(1, mBuilder.build());
    }

    private static void showNotificationMute(String notifyTips) {
        mBuilder = new NotificationCompat.Builder(App.sCx)
                .setSmallIcon(R.mipmap.ic_launcher_white)
                .setColor(App.sCx.getResources().getColor(R.color.accent))
                .setLargeIcon(BitmapFactory.decodeResource(App.sCx.getResources(), R.mipmap.ic_launcher))
                .setContentTitle(App.sCx.getString(R.string.iot_main_title))
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentText(notifyTips);
        Intent resultIntent = new Intent(App.sCx, MainActivity.class);
        //去掉 single task 类型的  launch mode 之后通过通知栏打开 activity 会出现多个 activity 实例, 所以需要 clear top , 暂不清楚有何风险
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(App.sCx, 0, resultIntent, 0);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(1, mBuilder.build());
    }

    public static void notifyUpdateResult() {
        String notifyType = CustomConfig.getInstance().notifyType;
        if (BuildConfig.FLAVOR.contains("Service")) {
            //无屏版本只能使用通知栏提示
            notifyType = CustomConfig.NotifyType.NOTIFICATION_NOTIFY.getType();
        }
        if (CustomConfig.NotifyType.DIALOG_NOTIFY.getType().equals(notifyType)) {
            showNotifyDialog(NoticeActivity.NOTICE_TYPE_SHOW_INSTALL_SUCCESS);
        } else {
            showNotification(App.sCx.getString(R.string.update_success));
        }
    }


    public static void notifyUpgrading(int progress) {
        showNotificationMute(App.sCx.getString(R.string.updating) + progress + "%");
    }

    public static void notifyDownloading(int progress) {
        showNotificationMute(App.sCx.getString(R.string.download) + "..." + progress + "%");
    }

    public static void notifyDownloadFinish() {
        showNotificationMute(App.sCx.getString(R.string.task_finished));
    }

    public static void notifyDownloadFailed() {
        showNotificationMute(App.sCx.getString(R.string.task_failed));
    }

    public static void notifyAbupdateSuccess() {
        showNotification(App.sCx.getString(R.string.ab_update_success_tips));
    }

    public static void notifyAbupdateFailed() {
        showNotification(App.sCx.getString(R.string.tips_upgrade_validate_file_fail));
    }

    public static void removeNotify() {
        if (null != mBuilder && null != mNotificationManager) {
            mNotificationManager.cancel(1);
        }
    }
}
